﻿using MySystemSetup.BusinessDomain;
using MySystemSetup.EnumDomain;
using MySystemSetup.IGive;
using MySystemSetup.IGive.Dto;
using MySystemSetup.IRole.Dto;
using MySystemSetup.IUser;
using MySystemSetup.RBACDomain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using TodoApp.Dto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MySystemSetup.Give
{
    public class GiveInfoAppService: ApplicationService,IGiveInfoAppService
    {




      
        public readonly IRepository<RoleInfo, int> _roleRepository;
        public readonly IRepository<UserInfo, string> _userRepoistory;
        public readonly IRepository<UserRole, int> _userRoleRepository;
        public readonly IRepository<GiveInfo, int> _giveRepository;
        public readonly IRepository<RolePermission, int> _rolePerResRepository;
        public readonly IRepository<UserInfo, string>  _userRepository;
          public readonly IRepository<FieldInfo, int>  _fieldRepository;
        public GiveInfoAppService(IRepository<RoleInfo, int> roleRepository, IRepository<UserRole, int> userRoleRepository, IRepository<RolePermission, int> rolePerResRepository, IRepository<GiveInfo, int> giveRepository, IRepository<UserInfo, string> userRepoistory, IRepository<FieldInfo, int> fieldRepository)
        {
            _roleRepository = roleRepository;

            _userRoleRepository = userRoleRepository;
            _rolePerResRepository = rolePerResRepository;
            _giveRepository = giveRepository;
            _userRepoistory = userRepoistory;
            _fieldRepository = fieldRepository;
        }




        /// <summary>
        /// 分页显示栋舍
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="limit">页容量</param>
        /// <param name="giveType">栋舍类型</param>
        /// <returns>统一返回格式</returns>

        public async Task<SzrApiPageResult<GiveShowDto>> GetGiveList(int page, int limit, GiveType? giveType, string? gName,string uId)
      {



            var giveRes=await _giveRepository.GetListAsync();
          var elRes=   giveRes.WhereIf(giveType != null, c => c.GiveType == giveType).
                 WhereIf(gName != null, c => c.GiveName.Contains(gName));

            List<RoleInfo>? ress = await _roleRepository.GetListAsync();
            var userRole = await _userRoleRepository.GetListAsync(c=>c.UId==uId);
            var rolePer = await _rolePerResRepository.GetListAsync();


            var res = userRole.Join(rolePer, a => a.RoleId, b => b.Id,
                (a, b) => new { a, b });

            var rolePerRes = res.Join(ress, a => a.b.RoleId,
                b => b.Id, (a, b) => new { a, b })
                .OrderBy(c => c.b.RoleGrade).FirstOrDefault();



            var userList=await _userRepoistory.GetListAsync();


            var list = Enum.GetValues<GiveType>().Select(c => new
            {

                Id = (int)c,
                Name = c.ToString()
            }).ToList();

            return new SzrApiPageResult<GiveShowDto>()
            {
                code = 0,
                data = elRes.Join(userList,a=>a.GPrincipalId,b=>b.Id,(a,b)=>new { a,b}).
                Select(c => new GiveShowDto()
                {
                    Id=c.a.Id,
                    GiveName=c.a.GiveName,
                    GiveType=list.Where(m=>m.Id==(int)c.a.GiveType).Select(q=>q.Name ).FirstOrDefault(),
                    GPrincipalId=c.b.UserName,
                    Production=c.a.Production,
                    ButtonPermiss=rolePerRes.a.b.ButtonPermiss,
                    GStatu=c.a.GStatu,
                    CreateDate=c.a.CreateDate,
                    UpdateDate=c.a.UpdateDate,
                    CreatePerson=c.a.CreatePerson,
                    UpdatePerson=c.a.UpdatePerson,
                    


                }).Skip((page - 1) * limit).Take(limit).ToList(),
                count = elRes.Count(),
                page = (int)Math.Ceiling(elRes.Count() * 1.0 / limit)
            };





        }





        /// <summary>
        /// 获取栋舍类型枚举
        /// </summary>
        /// <returns>统一返回格式</returns>
         public async Task<SzrApiResult<GiveShowDto>> GetGiveTypeEnum()
        {





            var list = Enum.GetValues<GiveType>().Select(c => new
            {

                Id = (int)c,
                Name = c.ToString()
            }).ToList();


            return new SzrApiResult<GiveShowDto>()
            {

                code = 200,
                data = list
            };





        }

        /// <summary>
        /// 获取栋舍类型枚举
        /// </summary>
        /// <returns>统一返回格式</returns>
        public async Task<SzrApiResult<UserShowDto>> GetAllUser()
        {





            var list = await _userRepoistory.GetListAsync();
           

            return new SzrApiResult<UserShowDto>()
            {

                code = 200,
                data = list
            };





        }





        /// <summary>
        /// 添加栋舍信息
        /// </summary>
        /// <param name="dto">要添加的栋舍信息</param>
        /// <returns>统一返回格式</returns>
        public async Task<SzrApiResult<GiveAddDto>> AddGiveInfoAsync(GiveAddDto dto)
        {


            var userRes=await _userRepoistory.FindAsync(dto.Id);
            var giveInfo = new GiveInfo();
          
            giveInfo.GiveName = dto.GiveName;
           giveInfo.GiveType = dto.GiveType;
            giveInfo.GPrincipalId = dto.GPrincipalId;
            giveInfo.Production = dto.Production;
            giveInfo.CreatePerson = userRes.UserName;
            giveInfo.UpdatePerson = userRes.UserName;
            giveInfo.GStatu = dto.GStatu;
            giveInfo.CreateDate = DateTime.Now;
            giveInfo.UpdateDate = DateTime.Now;
            await _giveRepository.InsertAsync(giveInfo);



            await _giveRepository.InsertAsync(giveInfo);


            return new SzrApiResult<GiveAddDto>()
            {
                code = 200,
                msg = "添加成功"

            };
        }


        /// <summary>
        /// 添加栋舍信息
        /// </summary>
        /// <param name="dto">要添加的栋舍信息</param>
        /// <returns>统一返回格式</returns>
        public async Task<SzrApiResult<GiveAddDto>> AddFieldInfoAsync(FieldInfo dto)
        {




            await _fieldRepository.InsertAsync(dto);

            
            return new SzrApiResult<GiveAddDto>() { code = 200, msg = "添加成功" };
        }
    }
}
